<template>
  <div>
    <el-dialog title="配置规则" :visible="visible" width="45%" @update:visible="updateVisible"
               :close-on-click-modal="false" @open="openDialog" @close="updateVisible(false)">
      <el-form :model="reForm" label-position="left" label-width="100px" ref="reForm" :rules="reFormRules">
        <el-form-item label="正则名称" prop="label">
          <el-input v-model="reForm.label" maxlength="20" show-word-limit></el-input>
        </el-form-item>
        <el-form-item label="表达式" prop="value">
          <el-input type="textarea" v-model="reForm.value" :rows="8" maxlength="500" show-word-limit></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="updateVisible(false)">取 消</el-button>
        <el-button type="primary" @click="submit('reForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {createReApi, updateReApi, deleteReApi} from '@/api/api'

export default {
  name: "reForm",
  props: ['visible', 'row'],
  data() {
    return {
      dialogStatus: '',
      reForm: {
        label: '',
        value: ''
      },
      reFormRules: {
        label: [{required: true, message: '请输入正则名称', trigger: 'blur'}],
        value: [{required: true, message: '请输入表达式', trigger: 'blur'}],
      },
    }
  },
  methods: {
    /*打开本组件弹框的回调*/
    openDialog() {
      if (this.row === 1) {
        this.dialogStatus = "新建"
        this.reForm = {label: '', value: ''}
      } else {
        this.dialogStatus = "编辑"
        this.reForm = this.row
      }
      this.$nextTick(() => {
        this.$refs['reForm'].clearValidate()
      })
    },
    /*更新本组件弹框状态*/
    updateVisible(val) {
      this.$emit('update:visible', val)
    },
    /*新建，编辑*/
    submit(reForm) {
      this.$refs[reForm].validate(async valid => {
        if (valid) {
          let param
          param = {...this.reForm}
          if (this.dialogStatus === "新建") {
            let resp = await createReApi(param)
            this.$message({message: resp, type: 'success'})
          } else {
            let resp = await updateReApi(param)
            this.$message({message: resp, type: 'success'})
          }
          this.updateVisible(false)
          this.$emit('refreshList', true)
        }
      });
    }
  }
}
</script>

<style scoped>

</style>
